$(function () {
    chinese.sSearch = "";
    var table = $('#commodityList').DataTable({
        "processing": true,
        "serverSide": true,
        "bSort": false,
         "ajax": {
            "url": "pointsystem/getCommodityList",
            "data": function (d) {
                d.cNameQuery = d.search.value;
            }
        },
        "aoColumnDefs": [{
                "targets": 0,
                "bSortable": false,
                "searchable": false,
                "className": "text-center",
                "mRender": function (data, type, row) {
                    return '<input type="checkbox" />';
                }
            }, {
                "targets": 1,
                "mRender": function (data, type, row) {
                    return '<a href="pointsystem/commodityindex/' + row.id + '" target="_blank">'+row.cname+'</a>';
                }
            }, {
                "targets": 2,
                "mRender": function (data, type, row) {
                    return row.unit;
                }
            }, 
            {
                "targets": 3,
                "mRender": function (data, type, row) {
                    return sysCommodityType[row.type];
                }
            },
            {
                "targets": 4,
                "mRender": function (data, type, row) {
                    return row.description;
                }
            }, {
                "targets": 5,
                "mRender": function (data, type, row) {
                    return row.points;
                }
            }, 

            {
                "targets": 6,
                "searchable": false,
                "mRender": function (data, type, row) {
                    return row.recordTime !== null
                        ? $.format.date(new Date(row.recordTime), "yyyy/MM/dd HH:mm")
                        : '-';
                }
            }, {
                "targets": 7,
                "searchable": false,
                "mRender": function (data, type, row) {
                   return row.updateTime !== null
                        ? $.format.date(new Date(row.updateTime), "yyyy/MM/dd HH:mm")
                        : '-';
                }
            },
            {
                "targets": 8,
                "searchable": false,
                "mRender": function (data, type, row) {
                   return row.state == 1
                        ? "上线"
                        : "下线";
                }
            },
            {
                "targets": 9,
                "searchable": false,
                "bSortable": false,
                
                "mRender": function (data, type, row) {
                    var operations;
                    if(row.state==1){
                       operations = $("#operationHTML").html().replace(/#id/g, row.id).replace(/#comstate/g, row.state).replace("上线","下线");
                    }else{
                       operations = $("#operationHTML").html().replace(/#id/g, row.id).replace(/#comstate/g, row.state); 
                    }
                    return operations;
                }
            },

            {
                "targets": -1,
                "bSortable": false,
                "searchable": false,
                "mRender": function (data, type, row) {
                }
            }
        ],
        "order": [[9, "desc"]],
        "oLanguage": chinese,
        "iDisplayLength": 10,
        "aLengthMenu": [
            [10, 25, 50, -1],
            [10, 25, 50, "全部"]
        ],
        "sDom": '<"dt-panelmenu clearfix"l<"pull-right"Tf>r>t<"dt-panelfooter clearfix"ip>',
        "oTableTools": {
            "aButtons": [{
                    "sExtends": "copy",
                    "sButtonText": "复制",
                    "fnComplete": function (nButton, oConfig, oFlash, sFlash) {
                        alert('列表内容已复制到剪贴板');
                    }
                },
                {
                    "sExtends": "xls",
                    "sButtonText": "导出为Excel"
                }
            ],
            "sSwfPath": root_path + "/js/plugins/datatables/extensions/TableTools/swf/copy_csv_xls_pdf.swf"
        },
        "fnDrawCallback": function () {
            // 高亮显示搜索内容
            if ($("#userList_filter input").val() !== "") {
                $("#commodityList").highlight($("#userList_filter input").val());
            }
        }
    
    });
     // 选中/取消选择行
    $("#userList tbody").on("click", 'input[type=checkbox]', function (e) {
        e.stopPropagation();
        if ($(this).prop("checked")) {
            $(this).parents("tr").addClass("selected");
        } else {
            $(this).parents("tr").removeClass("selected");
        }
    });
    /**
     * 显示/隐藏列
     */
    $('label.toggle-label input[type=checkbox]').on('change', function (e) {
        var checked = $(this).prop("checked"),
            col_index = $(this).attr('data-column'),
            column = table.column(col_index);

        if (checked) {
            column.visible(true);
        } else {
            column.visible(false);
        }
    });

    // 选中全部行
    function selectAllRows(selectAll) {
        if (selectAll) {
            $('#userList tbody tr').removeClass("selected");
            $('#userList tbody tr').addClass("selected");
            $('#userList tbody input[type=checkbox]').prop("checked", true);
        } else {
            $('#userList tbody tr').removeClass("selected");
            $('#userList tbody input[type=checkbox]').prop("checked", false);
        }
    }
 });
 
 function setDefault(id,state) {
     var conf;
     var st;
     if(state ==0){
         conf ="上线";
         st = 1;
     }else{
         conf ="下线";
         st =0;
     }
    if (confirm('确定将该商品'+conf+'？')) {		
        $.get("pointsystem/updateState/",{"id":id,"state":st}, function (res) {
            if (res.success) {
                alert(res.message);
                location.reload();
            } else {
                alert("商品状态刷新失败");
            }
        });
    }
    return false;
}
